package com.example.Activity;

import com.util.adapter.MyFragmentAdapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ContactFragemntActivity01 extends Fragment {
	private ViewPager mViewPager;
	private MyFragmentAdapter mAdapter; // 定义适配器
	/**
	 * 上边的三个tabItem及布局
	 */
	private LinearLayout mTabLiaotian;
	private LinearLayout mTabFaxian;
	private LinearLayout mTabTongxunlu;

	private TextView mLiaotian;
	private TextView mFaxian;
	private TextView mTongxunlu;
	/**
	 * TabItem下面的线条
	 */
	private ImageView mTabLine;

	private int currentIndex; // 当前处于哪个fragment
	private int screenWidth; // 屏幕宽度

	View mView; // 解析布局

	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.contact_activity_layout, container, false);
		setupViews();
		initTabLine();
		return mView;
	}

	private void initTabLine() {
		// textview底下的图片
		mTabLine = (ImageView) mView.findViewById(R.id.id_tab_line);
		screenWidth = getResources().getDisplayMetrics().widthPixels;
		LinearLayout.LayoutParams params = (LayoutParams) mTabLine.getLayoutParams();
		params.width = screenWidth / 3;
		// 设置tabLine的宽度
		mTabLine.setLayoutParams(params);
		
		
	}

	private void setupViews() {
		mViewPager = (ViewPager) mView.findViewById(R.id.id_viewpager);
		// 三个布局
		mTabFaxian = (LinearLayout) mView.findViewById(R.id.id_tab_faxian_ly);
		mTabLiaotian = (LinearLayout) mView.findViewById(R.id.id_tab_liaotian_ly);
		mTabTongxunlu = (LinearLayout) mView.findViewById(R.id.id_tab_tongxunlu_ly);

		MyLayoutOnClickListener myLayoutOnClickListener = new MyLayoutOnClickListener();
		mTabFaxian.setOnClickListener(myLayoutOnClickListener);
		mTabLiaotian.setOnClickListener(myLayoutOnClickListener);
		mTabTongxunlu.setOnClickListener(myLayoutOnClickListener);

		// 布局上的三个TextView
		mFaxian = (TextView) mView.findViewById(R.id.id_faxian);
		mLiaotian = (TextView) mView.findViewById(R.id.id_liaotian);
		mTongxunlu = (TextView) mView.findViewById(R.id.id_tongxunlu);

		Fragment01 mFragment01 = new Fragment01();
		Fragment02 mFragment02 = new Fragment02();
		Fragment03 mFragment03 = new Fragment03();
		mAdapter = new MyFragmentAdapter(getChildFragmentManager());

		// 往adapter中添加数据
		mAdapter.addTab(mFragment01);
		mAdapter.addTab(mFragment02);
		mAdapter.addTab(mFragment03);

		// 为viewpager设置适配器
		mViewPager.setAdapter(mAdapter);
		// 为viewPager设置滚动监听
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			// 状态改变时接口回调触发
			public void onPageSelected(int position) {
				resetTextViewColor();
				currentIndex = position;
				switch (position) {
				case 0:
					mLiaotian.setTextColor(getResources().getColor(R.color.blue));
					break;
				case 1:
					mFaxian.setTextColor(getResources().getColor(R.color.blue));

					break;
				case 2:
					mTongxunlu.setTextColor(getResources().getColor(R.color.blue));

					break;
				}
			}
			
			/**
			 * @param positon
			 *            当前的位置
			 * @param positionOffset
			 *            偏移比(小于1)，向左滑动(1.0-0变化)，向右滑动(0-1.0变化)
			 * @param positionOffsetPixels
			 *            具体偏移的像素
			 */
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
				// 设置tabLine的左偏移
				if (currentIndex == 0 && position == 0)// 0->1
				{
					LinearLayout.LayoutParams lp = (LayoutParams) mTabLine.getLayoutParams();
					lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 3) + currentIndex
							* (screenWidth / 3));
					
					mTabLine.setLayoutParams(lp);
				} else if (currentIndex == 1 && position == 0) // 1->0
				{
					LinearLayout.LayoutParams lp = (LayoutParams) mTabLine.getLayoutParams();
					lp.leftMargin = (int) (-(1 - positionOffset) * (screenWidth * 1.0 / 3) + currentIndex
							* (screenWidth / 3));
					mTabLine.setLayoutParams(lp);

				} else if (currentIndex == 1 && position == 1) // 1->2
				{
					LinearLayout.LayoutParams lp = (LayoutParams) mTabLine.getLayoutParams();
					lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 3) + currentIndex
							* (screenWidth / 3));
					mTabLine.setLayoutParams(lp);
				} else if (currentIndex == 2 && position == 1) // 2->1
				{
					LinearLayout.LayoutParams lp = (LayoutParams) mTabLine.getLayoutParams();
					lp.leftMargin = (int) (-(1 - positionOffset) * (screenWidth * 1.0 / 3) + currentIndex
							* (screenWidth / 3));
					mTabLine.setLayoutParams(lp);
				}

			}

			/**
			 * 状态改变回调函数，取值为 
			 * SCROLL_STATE_SETTLING 
			 * SCROLL_STATE_DRAGGING
			 * SCROLL_STATE_IDLE
			 */
			public void onPageScrollStateChanged(int state) {
			}
		});
		// 设置默认TabItem,一般默认是item0，切换两次触发字体颜色改变
		mViewPager.setCurrentItem(2);
		mViewPager.setCurrentItem(0);

	}

	/**
	 * 每次状态改变将字体颜色重置
	 */
	private void resetTextViewColor() {
		mFaxian.setTextColor(getResources().getColor(R.color.black));
		mLiaotian.setTextColor(getResources().getColor(R.color.black));
		mTongxunlu.setTextColor(getResources().getColor(R.color.black));
	}

	private class MyLayoutOnClickListener implements OnClickListener {
		public void onClick(View v) {
			int id = v.getId();
			switch (id) {
			case R.id.id_tab_liaotian_ly:
				mViewPager.setCurrentItem(0);
				break;
			case R.id.id_tab_faxian_ly:
				mViewPager.setCurrentItem(1);
				break;
			case R.id.id_tab_tongxunlu_ly:
				mViewPager.setCurrentItem(2);
				break;

			default:
				break;
			}
		}

	}
}